/**
 * WebOrderModule.java
 * com.sftj.web.order.module
 * Copyright (c) 2017, 北京科技有限公司版权所有.
*/

package com.sftz.modules.minapp.order.module;

import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.sftz.assets.entities.CourseEntity;
import com.sftz.assets.entities.SeriesCoursesEntity;
import com.sftz.assets.enums.OrderTypeEnum;
import com.sftz.modules.minapp.order.form.OrderForm;
import com.sftz.modules.minapp.order.form.WebOrderForm;
import com.sftz.modules.minapp.order.service.WebOrderViewService;
import com.sftz.modules.minapp.user.cache.RedissionUserCache;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.db.dao.impl.DbDao;

/**
 * @author   赵硕
 */
@IocBean
@At("/wx/order")
public class WebOrderModule {
	@Inject
	private WebOrderViewService webOrderViewService;

	@Inject
	private DbDao dbDao;

	//专业下订单
	@At
	@Ok("json")
	public Object addOrderMajor(@Param("..") final WebOrderForm webOrderForm, @Param("..") final OrderForm form) {
		String token = form.getToken();
		Integer userId = RedissionUserCache.getUserId(token);
		//设置用户id
		webOrderForm.setUserId(userId);
		//专业id
		Integer productId = webOrderForm.getProductId();
		SeriesCoursesEntity fetch = dbDao.fetch(SeriesCoursesEntity.class, Cnd.where("id", "=", productId));
		//机构id
		webOrderForm.setCollegeId(fetch.getInsititutionId());
		//专业名称
		webOrderForm.setProductName(fetch.getName());
		//专业价格
		webOrderForm.setAmountOfMoney(fetch.getActualPrice());
		//订单类型（课程/专业）
		webOrderForm.setOrderType(OrderTypeEnum.COURSESERIES.intKey());
		Object obj = webOrderViewService.add(webOrderForm);
		return obj;
	}

	//课程下订单
	@At
	@Ok("json")
	public Object addOrderCourse(@Param("..") final WebOrderForm webOrderForm, @Param("..") final OrderForm form) {
		String token = form.getToken();
		Integer userId = RedissionUserCache.getUserId(token);
		//设置用户id
		webOrderForm.setUserId(userId);
		//课程id
		Integer productId = webOrderForm.getProductId();
		CourseEntity courseEntity = dbDao.fetch(CourseEntity.class, Cnd.where("id", "=", productId));
		if (Util.isEmpty(courseEntity)) {
			throw new IllegalArgumentException("此课程商品不存在，课程id为" + productId);
		}
		//课程名称
		webOrderForm.setProductName(courseEntity.getCourseName());
		//课程价格
		webOrderForm.setAmountOfMoney(courseEntity.getActualPrice());
		//订单类型（课程/专业）
		webOrderForm.setOrderType(OrderTypeEnum.COURSE.intKey());
		Object obj = webOrderViewService.add(webOrderForm);
		return obj;
	}

}
